package com.ruoyi.project.lesson.poster.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.lesson.lesson.service.ILessonService;
import com.ruoyi.project.lesson.poster.domain.LessonPoster;
import com.ruoyi.project.lesson.poster.service.ILessonPosterService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 课程海报Controller
 *
 * @author shixiangwen
 * @date 2020-12-07
 */
@Controller
@RequestMapping("/lesson/poster")
public class LessonPosterController extends BaseController {

    private String prefix = "lesson/poster";

    @Autowired
    private ILessonPosterService lessonPosterService;

    @Autowired
    private ILessonService lessonService;

    @RequiresPermissions("lesson:poster:view")
    @GetMapping("/{lessonId}")
    public String poster(@PathVariable("lessonId") Long lessonId, ModelMap mmap) {
        mmap.put("lesson", lessonService.selectLessonById(lessonId));
        return prefix + "/poster";
    }

    /**
     * 查询课程海报列表
     */
    @RequiresPermissions("lesson:poster:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(LessonPoster lessonPoster) {
        startPage();
        List<LessonPoster> list = lessonPosterService.selectLessonPosterList(lessonPoster);
        return getDataTable(list);
    }

    /**
     * 导出课程海报列表
     */
    @RequiresPermissions("lesson:poster:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(LessonPoster lessonPoster) {
        List<LessonPoster> list = lessonPosterService.selectLessonPosterList(lessonPoster);
        ExcelUtil<LessonPoster> util = new ExcelUtil<LessonPoster>(LessonPoster.class);
        return util.exportExcel(list, "poster");
    }

    /**
     * 新增课程海报
     */
    @GetMapping("/add/{lessonId}/{type}")
    public String add(@PathVariable("lessonId") Long lessonId, @PathVariable("type") Integer type, ModelMap mmap) {
        mmap.put("lessonId", lessonId);
        mmap.put("type", type);
        mmap.put("lesson", lessonService.selectLessonById(lessonId));
        mmap.put("sort", lessonPosterService.getMaxSort(lessonId));
        return prefix + "/add";
    }

    @GetMapping("/preview/{lessonId}")
    public String preview(@PathVariable("lessonId") Long lessonId, ModelMap mmap) {
        mmap.put("lessonId", lessonId);
        return prefix + "/preview";
    }

    /**
     * 新增保存课程海报
     */
    @RequiresPermissions("lesson:poster:add")
    @Log(title = "课程海报", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(LessonPoster lessonPoster) {
        return toAjax(lessonPosterService.insertLessonPoster(lessonPoster));
    }

    /**
     * 修改课程海报
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        LessonPoster lessonPoster = lessonPosterService.selectLessonPosterById(id);
        mmap.put("lessonPoster", lessonPoster);
        return prefix + "/edit";
    }

    /**
     * 修改保存课程海报
     */
    @RequiresPermissions("lesson:poster:edit")
    @Log(title = "课程海报", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(LessonPoster lessonPoster) {
        return toAjax(lessonPosterService.updateLessonPoster(lessonPoster));
    }

    /**
     * 删除课程海报
     */
    @RequiresPermissions("lesson:poster:remove")
    @Log(title = "课程海报", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(lessonPosterService.deleteLessonPosterByIds(ids));
    }
}
